package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Result;
import com.itheima.exception.DeleteCategoryException;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;

import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService CService;

    @Autowired
    private DishService dish;

    @Autowired
    private SetmealService setmeal;

    //http://localhost:8080/category/page?page=1&pageSize=10&name=???
    @GetMapping("/page")
    public Result page(int page, int pageSize, String name) {
        //1.将mp的分页拦截器交给SpringBoot

        //3.定义所需要的参数
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        IPage<Category> pages = new Page<>(page, pageSize);

        //2.调用分页的方法
        lqw.orderByAsc(Category::getSort);
        IPage<Category> iPage = CService.page(pages, lqw);

        //3.给前端响应数据  records total
        long total = iPage.getTotal();
        List<Category> records = iPage.getRecords();

        //4.将数据封装
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);

        return Result.success(map);
    }

    @PostMapping
    public Result saveCategory(@RequestBody Category category) {

        boolean save = CService.save(category);
        if (!save) {
            return Result.error("新增菜品分类失败");
        }

        return Result.success("新增菜品分类成功");
    }

    //Request URL: http://localhost:8080/category?ids=1454771429823242200
    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") long id) {
        Category one = CService.getById(id);

        Integer type = one.getType();

        if (type == 1) {
            LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Dish::getCategoryId, id);
            int count = dish.count(lqw);
            if (count > 0) {
                // return Result.error("该菜品下有菜品,不能被删除");
                throw new DeleteCategoryException("该菜品下有菜品,不能被删除");
            }
        } else {
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getCategoryId, id);
            int count = setmeal.count();
            if (count > 0) {
                //return Result.error("该菜品下有菜品,不能被删除");
                throw new DeleteCategoryException("该套餐下有套餐,不能被删除");
            }
        }

        boolean result = CService.removeById(id);

        if (!result) {
            return Result.error("删除失败");
        }

        return Result.success("删除成功");
    }

    @PutMapping
    public Result updateSingle(@RequestBody Category category, HttpServletRequest req) {

        boolean result = CService.updateById(category);

        if (!result) {
            return Result.error("修改失败");
        }

        return Result.success("修改成功");
    }

    //Request URL: http://localhost:8080/category/list?type=1
//    请求 URL: http://localhost:8080/category/list
//    请求方法: GET
    @GetMapping("/list")
    public Result listAdd(Integer type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type!=null,Category::getType, type);
        List<Category> list = CService.list(lqw);

        return Result.success(list);
    }
}
